<template>
  <div>
    <el-dialog
      :title="info.issAdd?'管理员编辑':'管理员添加'"
      :visible.sync="info.isshow"
      @closed="closed"
      width="60%"
    > {{form}}
      <el-form   :model="form">
       
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="form.roleid" placeholder="请选择">
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="提示" label-width="120px" v-if="!info.isAdd">密码不写则不修改</el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { successAlert } from "../../../utils/alert";
import {
  reqrolelist,
  requseradd,
  requseredit,
  requserinfo,
} from "../../../request/api";
import {mapGetters,mapActions} from "vuex"
export default {
  components: {},
  computed:{
    ...mapGetters({
      user:"info"
    })
  },
  mounted() {
    reqrolelist().then((res) => {
      if (res.data.code == 200) {
        this.rolelist = res.data.list;
      }
    });
  },
  // 接收父节组件参数
  props: ["list", "info"],
  //初始化数据
  data() {
    return {
      rolelist: [],
      form: {
        roleid:"",
        username:"",
        password:"",
        status:2
      },
      menulist: [],
    };
  },
  methods: {
    ...mapActions({
      changeInfo:"changeInfo"
    }),
    // 点击取消
    cancel() {
      this.info.isshow = false;
    },
    //清空数据
    empty() {
      this.form = {
        roleid:"",
        username:"",
        password:"",
        status:2
      };
    },
    // 点击添加
    add() {
      requseradd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 弹提示
          successAlert(res.data.msg);
          // 弹框消失
          this.cancel();
          //清空数据
          this.empty();
          //刷新列表
          this.$emit("init");
        }
      });
    },
    getOne(id) {
      requserinfo({ uid: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.password = '';
        }
       
      });
    },
    // form关闭
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    //点击修改
    update() {
      
      requseredit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);

          ///如果修改的管理就是当前登录的账号，那么需要重新登录
          if(this.user.uid===this.form.uid){
            //清除用户数据
            this.changeInfo({})
            //去登录
            this.$router.replace("/login")
            return;
          }
          this.$emit("init");
          this.cancel();
          this.empty();
          
        }
      });
    },
  },
};
</script>
<style lang="less">
.fl {
  float: left;
  width: 100px;
}
.el-input {
  float: left;
  width: 400px;
}
.con {
  overflow: hidden;
}
.el-dialog__header {
  width: 50%;
}
</style>